﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace FeatureServer.Test
{
    public partial class FrmFeatureServerTest : Form
    {
        public FrmFeatureServerTest()
        {
            InitializeComponent();
        }

        private void btnFeaturServer_Click(object sender, EventArgs e)
        {
            var root = new GISServer.Core.Client.FeatureService.Root();
            root.ServiceDescription = "Edit parcels, buildings and owner information.";
            root.Layers.Add(new GISServer.Core.Client.FeatureService.Layer
            {
                Id = 0,
                Name = "Parcels"
            });
            root.Layers.Add(new GISServer.Core.Client.FeatureService.Layer
            {
                Id = 1,
                Name = "Buildings"
            });
            root.Tables.Add(new GISServer.Core.Client.FeatureService.Table
            {
                Id = 3,
                Name = "Owners"
            });

            var str = root.ToJson();
        }

        private void featurebtn_Click(object sender, EventArgs e)
        {
            var feature = new GISServer.Core.Client.Feature();



            var polygon = new GISServer.Core.Client.Geometry.Polygon();

            var pointcollection = new GISServer.Core.Client.Geometry.PointCollection();


            pointcollection.Add(new GISServer.Core.Client.Geometry.Point(-97.06138, 32.837));
            pointcollection.Add(new GISServer.Core.Client.Geometry.Point(-97.06133, 32.836));
            pointcollection.Add(new GISServer.Core.Client.Geometry.Point(-97.06124, 32.834));
            pointcollection.Add(new GISServer.Core.Client.Geometry.Point(-97.06138, 32.837));
            pointcollection.Add(new GISServer.Core.Client.Geometry.Point(-97.06127, 32.832));
            pointcollection.Add(new GISServer.Core.Client.Geometry.Point(-97.06138, 32.837));

            polygon.Rings.Add(pointcollection);

            feature.Geometry = polygon;

            feature.Attributes = new GISServer.Core.Client.Attributes("hi1", "value1", "hi2", "value2");
            //var Sp = new List<Dictionary<string, string>>();
            //Sp.Add(new Dictionary<string, string>() { { "hi", "value" } });
            //Sp.Add(new Dictionary<string, string>() { { "hi1", "value1" } });
            //feature.Attributes = Sp;

            var str = feature.ToJson();

        }

        private void btnlayerortable_Click(object sender, EventArgs e)
        {
            var color = new List<int>();
            color.Add(156);
            color.Add(173);
            color.Add(0);
            color.Add(255);

            var symbol = new GISServer.Core.Client.Symbols.SimpleMarkerSymbol
            {
                Type = "esriSMS",
                Style = "esriSMSCircle",
                Color=color,
                Size = 4,
                Angle = 0,
                Xoffset = 0,
                Yoffset = 0,
                Outline=new GISServer.Core.Client.Symbols.Outline(1,156,173,0,255)
            };

            

            var lyr = new GISServer.Core.Client.LayerOrTable
                {
                    CurrentVersion = 1.1,
                    Id = 1,
                    Name = "Cities",
                    Type = "Feature Layer",
                    Description = "This service presents census information for U.S",
                    DefinitionExpression = "",
                    GeometryType = "esriGeometryPoint",
                    CopyrightText = "(c)",
                    ParentLayer = null,
                    SubLayers = null,
                    MinScale = 0,
                    MaxScale = 0,
                    DefaultVisibility = true,
                    Extent = new GISServer.Core.Client.Geometry.Envelope(-158.064605712891, 19.6961498260498, -67.986768734036, 64.8691042579708, new GISServer.Core.Client.Geometry.SpatialReference(4326)),
                    HasAttachments = false,
                    HtmlPopupType = "esriServerHTMLPopupTypeNone",
                    DrawingInfo = new GISServer.Core.Client.DrawingInfo
                    {
                        Renderer = new GISServer.Core.Client.SimpleRenderer
                {
                    Type = "simple",
                    Symbol=symbol,
                    Label = "",
                    Description = ""
                },
                        Transparency = 0,
                        LabelingInfo = null
                    }
                };
            var str = lyr.ToJson();
        }

        private void btnfetures_Click(object sender, EventArgs e)
        {
            var fs = new GISServer.Core.Client.FeatureSet();
            fs.DisplayFieldName = "CITY_NAME";
            var fieldalias = new GISServer.Core.Client.Attributes();
            fieldalias.AttributeCollection = new List<GISServer.Core.Client.Attribute>();
            fieldalias.AttributeCollection.Add(new GISServer.Core.Client.Attribute
            {
                Key = "CITY_NAME",
                Value = "CITY_NAME"
            });

            fs.FieldAliases = fieldalias;


            fs.GeometryType = "esriGeometryPoint";
            fs.SpatialReference = new GISServer.Core.Client.Geometry.SpatialReference(4326);
            fs.Fields.Add(new GISServer.Core.Client.Field
            {
                Name = "CITY_NAME",
                Type = "esriFieldTypeString",
                Alias = "CITY_NAME",
                Length=40
            });
            


            var feature = new GISServer.Core.Client.Feature();
            feature.Geometry = new GISServer.Core.Client.Geometry.Point(-122.46818353792992, 48.743879854364899);
            feature.Attributes= new GISServer.Core.Client.Attributes( "CITY_NAME", "Bellingham");

            var feature1 = new GISServer.Core.Client.Feature();
            feature1.Geometry = new GISServer.Core.Client.Geometry.Point(-109.67985528815009, 48.543818264018633);
            feature1.Attributes= new GISServer.Core.Client.Attributes( "CITY_NAME", "Havre");

            var feature2 = new GISServer.Core.Client.Feature();
            feature2.Geometry = new GISServer.Core.Client.Geometry.Point(-122.63068540357024, 48.49221584868156);
            feature2.Attributes= new GISServer.Core.Client.Attributes( "CITY_NAME", "Anacortes");

            fs.Features.Add(feature);
            fs.Features.Add(feature1);
            fs.Features.Add(feature2);

            var str = fs.ToJson();
        }

        private void btnquery_Click(object sender, EventArgs e)
        {
            var fs = new GISServer.Core.Client.FeatureSet();
            fs.DisplayFieldName = "AREANAME";
            fs.FieldAliases = new GISServer.Core.Client.Attributes("ST", "State Name", "POP2000","Population","AREANAME","City Name");
            fs.GeometryType = "esriGeometryPoint";
            fs.SpatialReference = new GISServer.Core.Client.Geometry.SpatialReference(4326);

            var feature = new GISServer.Core.Client.Feature();
            feature.Geometry = new GISServer.Core.Client.Geometry.Point(-118.4, 34.1);
            feature.Attributes = new GISServer.Core.Client.Attributes("ST", "WA", "POP2000", "563374", "AREANAME", "Seattle");

            fs.Features.Add(feature);
            var st = fs.ToJson();
        }

    }
}
